package ctrconfig

import (
	"github.com/astaxie/beego/logs"
	"fmt"
	"cloud_worm/util/uhttp"
	"io/ioutil"
	"encoding/json"
	"cloud_worm/util/umodels/mysqldb/dbmanager"
	"cloud_worm/util/uid"
	"time"
)

type UserController struct {
	LoginCtr
}

func (l *UserController) Get()  {
	l.Guest()

	userInfo := l.UserInfo
	logs.Debug(fmt.Sprintf("%v", userInfo))
	l.Data["json"] = uhttp.CreateOkResponse(userInfo)
	l.ServeJSON()
	return
}

func (l *UserController) Logout()  {
	l.Guest()

	l.logoutInner()
	l.Data["json"] = uhttp.CreateOkResponse(nil)
	l.ServeJSON()
	return
}

func (l *UserController) ChangeInfo()  {
	l.Guest()

	type UserInfo struct {
		Id 				int64		`json:"id"`
		Avatar 			string		`json:"avatar"`
	}
	body, err := ioutil.ReadAll(l.Ctx.Request.Body)
	if err != nil {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeReqBodyErr, err.Error(), nil)
		l.ServeJSON()
		return
	}
	var input UserInfo
	if err = json.Unmarshal(body, &input); err != nil {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeReqBodyErr, "input Unmarshal error.", nil)
		l.ServeJSON()
		return
	}

	webUser := &dbmanager.WebUser{Id: input.Id}
	if err := webUser.Read("id"); err != nil {
		logs.Warn(fmt.Sprintf("edit input id error: %s.", err.Error()))
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input id error.", nil)
		l.ServeJSON()
		return
	}

	webUser.Avatar = input.Avatar
	webUser.Updated = time.Now().Unix()
	if err := webUser.Update("avatar", "updated"); err != nil {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "update error.", nil)
		l.ServeJSON()
		return
	}

	l.Data["json"] = uhttp.CreateOkResponse(nil)
	l.ServeJSON()
	return


}

func (l *UserController) ChangePassword()  {
	l.Guest()

	type UserInfo struct {
		Id 			int64		`json:"id"`
		OldPwd 		string		`json:"old_pwd"`
		NewPwd1		string		`json:"new_pwd_1"`
		NewPwd2 	string		`json:"new_pwd_2"`
	}
	body, err := ioutil.ReadAll(l.Ctx.Request.Body)
	if err != nil {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeReqBodyErr, err.Error(), nil)
		l.ServeJSON()
		return
	}
	var input UserInfo
	if err = json.Unmarshal(body, &input); err != nil {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeReqBodyErr, "input Unmarshal error.", nil)
		l.ServeJSON()
		return
	}

	logs.Info(input)
	webUser := &dbmanager.WebUser{Id: input.Id}
	if err := webUser.Read("id"); err != nil {
		logs.Warn(fmt.Sprintf("edit input id error: %s.", err.Error()))
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input id error.", nil)
		l.ServeJSON()
		return
	}
	if uid.PasswordMatch(webUser.Password, input.OldPwd) == false {
		logs.Warn(fmt.Sprintf("edit input old password error."))
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input old password error.", nil)
		l.ServeJSON()
		return
	}
	if input.NewPwd1 != input.NewPwd2 || input.NewPwd2 == "" {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input new password diff error.", nil)
		l.ServeJSON()
		return
	}

	secret, err := uid.PasswordHash(input.NewPwd1)
	if err != nil {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "input password error.", nil)
		l.ServeJSON()
		return
	}

	webUser.Password = secret
	webUser.Updated = time.Now().Unix()
	if err := webUser.Update("password", "updated"); err != nil {
		l.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "update error.", nil)
		l.ServeJSON()
		return
	}

	l.Data["json"] = uhttp.CreateOkResponse(nil)
	l.ServeJSON()
	return
}